package com.reggie.background.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.background.common.R;
import com.reggie.background.dto.SetmealDto;
import com.reggie.background.entity.Category;
import com.reggie.background.entity.Setmeal;
import com.reggie.background.entity.SetmealDish;
import com.reggie.background.service.CategoryService;
import com.reggie.background.service.SetmealDishService;
import com.reggie.background.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐，并保存套餐与菜品关系
     *
     * @param setmealDto
     * @return
     */
    @CacheEvict(value="setmealCache",allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        Boolean result = setmealService.saveSetmealAndSetmealDish(setmealDto);
        if (result) {
            return R.success("成功添加套餐！");
        }
        return R.success("添加套餐失败！");
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
        //根据page pageSize，name先查询套餐数据库表
        LambdaQueryWrapper<Setmeal> query = new LambdaQueryWrapper<>();
        query.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        query.orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> pageinfo = new Page<>(page, pageSize);
        setmealService.page(pageinfo, query);
        //将查询出来的分页结果重新封装到带有套餐分类名的前端model分页
        Page<SetmealDto> result = new Page<>();
        BeanUtils.copyProperties(pageinfo,result,"records");
        List<SetmealDto> collect = pageinfo.getRecords().stream().map(item -> {
            //将Setmeal转换成setmealDao并查询categoryName
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        result.setRecords(collect);
        return R.success(result);
    }

    /**
     * 修改套餐的状态
     * @param status
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status, Long[] ids) {
        List<Setmeal> collect = Arrays.stream(ids).map(item -> {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(item);
            return setmeal;
        }).collect(Collectors.toList());
        boolean result = setmealService.updateBatchById(collect);
        if (result) {
            return R.success("套餐状态更改成功！");
        }
        return R.error("套餐状态更改失败！");

    }

    /**
     * 根据id查询Setmealdao
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getOneById(@PathVariable Long id) {
        //先根据id查询setmeal
        Setmeal setmeal = setmealService.getById(id);
        //根据setmeal的id查询SetmealDish集合
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(qw);
        //将setmeal和setmealdish集合封装到setmealDao返回到前端model
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(list);

        return R.success(setmealDto);
    }

    /**
     * 更新套餐信息已经套餐与菜品关系
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PutMapping
    public R<String> updateSetmealDao(@RequestBody SetmealDto setmealDto) {
        Boolean result = setmealService.updateSetmealAndSetmealDish(setmealDto);
        if (result) {
            return R.success("套餐信息已经更新！");
        }
        return R.error("套现信息更新失败！");
    }

    /**
     * 根据id集合删除套餐信息
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> deleteSetmeal(Long[] ids){
        Boolean result = setmealService.deleteSetmeals(ids);
        if (result) {
            return R.success("套餐删除成功！");
        }
        return R.error("套餐删除失败！");
    }
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status")
    public R<List<Setmeal>> list(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(qw);
        return  R.success(list);
    }


}
